


#ifndef _I7816_DEFINE_H_
#define _I7816_DEFINE_H_

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "i7816_status.h"

#define FIDI_DEFAULT_VAL        0x11
typedef enum _i7816_icc_state{
    ICC_ST_ABSENT = 0,      //未出现
    ICC_ST_PRESENT = 1,     //出现
    ICC_ST_ACTIVATED = 2,    //激活
    ICC_ST_PRESENT_ACTIVATE = 3,       //出现并激活
} i7816_icc_state_t;

typedef enum _i7816_class{
    ICC_CLASS_A = 0x01,
    ICC_CLASS_B = 0x02,
    ICC_CLASS_C = 0x04,
    // ICC_CLASS_ANY = ICC_CLASS_A|ICC_CLASS_B|ICC_CLASS_C,
}i7816_class_t;

typedef enum _reset_mode{
    RESET_MODE_COLD = 0,
    RESET_MODE_WARM = 1,
}reset_mode_t;

typedef enum _i7816_protocol{
    PROTOCOL_T0 = 0,
    PROTOCOL_T1 = 1,
    PROTOCOL_ANY = 2,
}i7816_protocol_t;

typedef enum _transfer_dir {
	TRANSFER_OUT = 0,
    TRANSFER_IN = 1
}transfer_dir_t;
/*
GT = 12etu + R * N / f
*/
typedef struct _i7816_guard_time {
    uint32_t extra_clk;     // GT中的扩展clk部分：R * N / f
    uint8_t etu;            //公式中的：12etu部分。从字符结束处计算，不是ISO7816中定义是从字符开始处计算
}i7816_guard_time_t;

#define ATR_MAX_LEN     32

typedef struct _i7816_hw_param {
    uint32_t last_time;         //前一字节传输(收/发)的时间点
    i7816_guard_time_t GT;      //保护时间配置
    i7816_class_t type;         //电压类型
    i7816_protocol_t protocol;  //协议类型  
    bool inverse;               //是否反序
    uint8_t FiDi;               //波特率分频参数，7816协议的FiDi
}i7816_hw_param_t;

typedef struct _i7816_icc{
    //协议相关参数
    uint32_t WWT;				//T=0,工作等待时间

	uint32_t BWT;				//T=1,块等待时间
	uint32_t CWT;				//T=1,块内字节间等待时间
	uint8_t IFSC;				//T=1,信息块最大传输数据量
	uint8_t SN_out;             //T=1,输出SN
	uint8_t SN_in;              //T=1,输入SN
    uint8_t IFSD;             //T=1,同步IFS标志
    uint8_t error_countor;
    
    uint8_t alen;               //atr长度
    uint8_t atr[ATR_MAX_LEN];   //atr缓存
}i7816_icc_t;

typedef struct _i7816_hw{
    const void* con;
    void* usart;
    uint32_t freq;              //clk引脚频率
    i7816_hw_param_t* param;
    uint8_t id;
}i7816_hw_t;

typedef struct _i7816_ifd{
    i7816_icc_state_t state;  //icc状态
    i7816_icc_t icc;        //icc
    i7816_hw_param_t param;
    i7816_hw_t* hw;
}i7816_ifd_t;

#endif	//_I7816_DEFINE_H_


